package com.jspgou.cms.action.front;

import static com.jspgou.common.page.SimplePage.cpn;
import static com.jspgou.cms.Constants.SHOP_SYS;

import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import com.jspgou.common.page.Pagination;
import com.jspgou.common.web.springmvc.MessageResolver;
import com.jspgou.core.entity.Website;
import com.jspgou.core.web.front.FrontHelper;
import com.jspgou.cms.entity.ShopPurchase;
import com.jspgou.cms.manager.ShopPurchaseMng;
import com.jspgou.cms.web.ShopFrontHelper;
import com.jspgou.cms.web.SiteUtils;

@Controller
public class ShopPurchaseAct {

	private static final String PURCHASELIST = "tpl.purchaselist";
	private static final String PURCHASEADD = "tpl.purchaseadd";
	private static final String UPDATEPURCHASE = "tpl.updatepurchase";

	/**
	 * 采购单list
	 * 
	 * @param pageNo
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/purchaselist.jspx")
	public String list(Integer pageNo, HttpServletRequest request,
			ModelMap model, int merchantid) {
		Pagination pagination = manager.getShopPurchasePage(merchantid + "",
				cpn(pageNo), 5);
		
		model.addAttribute("pagination", pagination);
		model.addAttribute("merchantid", merchantid);
		Website web = SiteUtils.getWeb(request);
		return web.getTplSys(SHOP_SYS, MessageResolver.getMessage(request,
				PURCHASELIST), request);
	}

	/**
	 * 添加采购单页面
	 * 
	 * @param model
	 * @param merchantid
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/purchaseadd.jspx")
	public String add(ModelMap model, int merchantid, HttpServletRequest request) {
		model.addAttribute("merchantid", merchantid);
		Website web = SiteUtils.getWeb(request);
		ShopFrontHelper.setCommonData(request, model, web);
		return web.getTplSys(SHOP_SYS, MessageResolver.getMessage(request,
				PURCHASEADD), request);
	}

	/**
	 * 保存采购单
	 * 
	 * @param bean
	 * @param request
	 * @param model
	 * @param merchantid
	 * @return
	 */
	@RequestMapping(value = "/purchasesave.jspx")
	public String save(ShopPurchase bean, HttpServletRequest request,
			ModelMap model, int merchantid) {
		bean.setMerchantid(merchantid + "");
		bean.setTime(new Date());
		
		bean = manager.save(bean);
		Website web = SiteUtils.getWeb(request);
		return FrontHelper.showSuccess("操作成功", "purchaseadd.jspx?merchantid="
				+ merchantid, web, model, request);
	}
	
	/**
	 * 删除采购单
	 * 
	 * @param id
	 * @param pageNo
	 * @param request
	 * @param model
	 * @param merchantid
	 * @return
	 */
	@RequestMapping(value = "/purchasedelete.jspx")
	public String delete(int id, Integer pageNo, HttpServletRequest request,
			ModelMap model, Integer merchantid) {
		if (!(id + "").equals("")) {
			manager.deleteById(Long.parseLong(id + ""));
		}
		return list(pageNo, request, model, merchantid);
	}
	
	
	/**
	 * 修改商品页面
	 * 
	 * @param id
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/updatepurchase.jspx")
	public String updatepurchase(Long id, HttpServletRequest request,
			ModelMap model, Integer merchantid) {
		model.addAttribute("merchantid", merchantid);
		model.addAttribute("shopPurchase", manager.findById(id));
		Website web = SiteUtils.getWeb(request);
		ShopFrontHelper.setCommonData(request, model, web);
		return web.getTplSys(SHOP_SYS, MessageResolver.getMessage(request,
				UPDATEPURCHASE), request);
	}
	
	
	/**
	 * 采购单修改保存
	 * 
	 * @param bean
	 * @param pageNo
	 * @param request
	 * @param model
	 * @param merchantid
	 * @return
	 */
	@RequestMapping(value = "/updatesavepurchase.jspx")
	public String updatesaveCommodity(ShopPurchase bean, Integer pageNo,
			HttpServletRequest request, ModelMap model, int merchantid) {
		model.addAttribute("merchantid", merchantid);
		
		bean = manager.update(bean);
		return list(pageNo, request, model, merchantid);
	}

	@Autowired
	private ShopPurchaseMng manager;
}
